/**
 * 权限名称的集合的过滤组合处理
 * authSources <object>：数据源
 * @createTime: 2020-01-06
 * @email: cvper@sina.com
 */ 
function AuthFilter(authSources){
  let permissionsObj = []
  let loopData = provideModuleSources(authSources.grantedPolicies)
  for (let i in loopData) {
    let obj = {
      "moduleName": cutModuleName(i),
      "moduleShow": loopData[i]
    }
    permissionsObj.push(obj)
  }
  return permissionsObj
}

// 获取一级 module 数据源
function provideModuleSources(authSources){
  let tempResultArray = {}
  for(let i in authSources){
    if(i.match(/\./g).length === 2){
      tempResultArray[i] = authSources[i]
    }
  }
  return tempResultArray
}

/**
 * 截取模块的名称处理，例如：APP.module.example 经处理获得 example 名称
 * str <string>：当前模块全名称
 * @createTime: 2020-01-06
 * @email: cvper@sina.com
 */
function cutModuleName(str){
  let tempStr = str
  let pattern = /(?<=.)[^.]+(?=$)/.exec(tempStr)
  return pattern[0]
}

/**
 * 获取当前模块下的所有页面，生成权限对象
 * authSources <object>：权限配置对象数据源
 * @createTime: 2020-01-07
 * @email: cvper@sina.com
 */
function getModulePage(authSources){
  let pageName = ''
  let tempPage = []
  let fullModuleName = ''  
  let moduleName = ''
  let tempResult = []
  let tempPower = {}
  for(let i in authSources){
    if(i.match(/\./g).length === 3){
      tempPage.push(i)
    }else if(i.match(/\./g).length === 4){
      tempPower[i] = authSources[i]
    }
  }

  tempPage.map((item, index)=>{
    pageName = (/(?<=.)[^.]+(?=$)/.exec(item))[0]
    moduleName = item.split('.')[2]
    fullModuleName = item
    let reg = new RegExp(pageName)
    let powerObj = singlePagePowerFilter(reg, tempPower)
    let tempObj = {
      fullModule: fullModuleName,
      moduleName: moduleName,
      page: pageName,
      power: powerObj,
    }
    tempResult.push(tempObj)
  })

  

  console.log("tempResult--998800", tempResult);
  return tempResult
}

/**
 * 单页面权限数据过滤处理 
 * reg <RegExp object>：匹配当前页面的正则
 * power <object>：页面权限集合
 * @createTime: 2020-01-07
 * @email: cvper@sina.com
 */
function singlePagePowerFilter(reg, power){
  let currentPage = {}
  for(let i in power){
    if((i.match(/\./g).length === 4) && (i.search(reg) !==-1)){
      currentPage[cutModuleName(i)] = power[i]
    }
  }
  return currentPage
}

/**
 * 创建 permissions 对象数组
 * arr <array>: 
 * @createTime: 2020-01-07
 * @email: cvper@sina.com
 */
function createPermissons(arr){
  let tempArr = arr
  let resultArr = [];
  tempArr.map((item, index)=>{
    let tempObj = {
      'roleId': 'admin',
      'permissionId': item.moduleName,
      'permissionName': '',
      'actions': '[]', // 此数据临时空 暂无使用数组对象
      'actionEntitySet': [],
      'actionList': null,
      'dataAccess': null
    }  
    resultArr.push(tempObj)
  })
  return resultArr
}

export const structureTransform = ( origin ) => {
  const originSources = origin
  let authSources = originSources.auth
  let permissionsObj = AuthFilter(authSources)
  let tempPermissions = createPermissons(permissionsObj)

  console.log("tmd-tmd-tmd", authSources.grantedPolicies);
  
  let tempActionChildren = getModulePage(authSources.grantedPolicies)
  
  /*
    第一层的基本的参数对应表
    id - 用户id
    name - 用户昵称
    username - 用户名
    password - 密码
    avatar - 头像
    status - 状态
    telephone - 手机号码
    lastLoginIp - 最近登陆 ip
    lastLoginTime - 最近登陆时间
    creatorId - 创建人
    createTime - 创建时间
    merchantCode - 租户id
    deleted -  ~
    roleId - 角色id
    role - 角色
  */
  const newPermissonStructure = {
    'id': originSources.currentUser.id,
    'name': originSources.currentUser.nickName?originSources.currentUser.nickName:'测试 nickname',
    'username': originSources.currentUser.userName?originSources.currentUser.userName:'无敌userName测试处理',
    'password': '',
    'avatar': '/avatar2.jpg',
    'status': 1,
    'telephone': originSources.currentUser.telephone? originSources.currentUser.telephone : '', 
    'lastLoginIp': originSources.currentUser.lastLoginIp? originSources.currentUser.lastLoginIp : '',
    'lastLoginTime': originSources.currentUser.lastLoginTime? originSources.currentUser.lastLoginTime : '',
    'creatorId': originSources.currentUser.creatorId? originSources.currentUser.creatorId : '',
    'createTime': originSources.currentUser.createTime? originSources.currentUser.createTime : '',
    'merchantCode': originSources.currentUser.tenantId? originSources.currentUser.tenantId : '',
    'deleted': 0,
    'roleId': 'admin',
    'role': 'admin',
    'email': originSources.currentUser.email?originSources.currentUser.email:'cvper@sina.com',
  }

  // 创建 role d对象 - 1
  const roleObj = {
    'id': 'admin',
    'name': '管理员',
    'describe': '拥有所有权限',
    'status': 1,
    'creatorId': 'system',
    'createTime': 1497160610259,
    'deleted': 0,
    'permissions': tempPermissions,
    'actionChildren': tempActionChildren,
    'currentPage': '',
  }

  // 完整的 role 对象
  // 'permissions': [
  //   {
  //     'roleId': 'admin',
  //     'permissionId': 'dashboard',
  //     'permissionName': '主页',
  //     'actions': '[]', // 此数据临时空 暂无使用数组对象
  //     'actionEntitySet': [],
  //     'actionList': null,
  //     'dataAccess': null
  //   },
  //   {
  //     'roleId': 'admin',
  //     'permissionId': 'example',  // 测试 - e1 -页面
  //     'permissionName': '例子',
  //     'actions': '[]',
  //     'actionEntitySet': [  // 单个模块的下级所有页面统一权限控制
  //       { "action": "add", "describe": "新增", "defaultCheck": true},
  //       { "action": "delete", "describe": "删除", "defaultCheck": true },
  //       { "action": "update", "describe": "修改", "defaultCheck": true },
  //       { "action": "query", "describe": "查询", "defaultCheck": true },
  //       { "action": "export", "describe": "导出", "defaultCheck": true },
  //       { "action": "reload", "describe": "刷新", "defaultCheck": true },
  //       { "action": "seniorSearch", "describe": "高级查询", "defaultCheck": true },
  //       { "action": "column", "describe": "列", "defaultCheck": true },
  //       { "action": "cardView", "describe": "卡片视图", "defaultCheck": true }
  //     ],
  //     'actionChildren':[ // 单个模块的具体下级页面的权限控制，可覆盖模块级的配置
  //       {
  //         pathName: '/example/e1',
  //         name: 'E1',
  //         actionList: [
  //           { "action": "add", "describe": "新增", "defaultCheck": true},
  //           { "action": "delete", "describe": "删除", "defaultCheck": true },
  //           { "action": "update", "describe": "修改", "defaultCheck": true },
  //           { "action": "query", "describe": "查询", "defaultCheck": true },
  //           { "action": "export", "describe": "导出", "defaultCheck": true },
  //           { "action": "reload", "describe": "刷新", "defaultCheck": true },
  //           { "action": "seniorSearch", "describe": "高级查询", "defaultCheck": true },
  //           { "action": "column", "describe": "列", "defaultCheck": true },
  //           { "action": "cardView", "describe": "卡片视图", "defaultCheck": true }
  //         ]
  //       },
  //       {
  //         pathName: '/example/e3',
  //         name: 'E2',
  //         actionList: [
  //           { "action": "add", "describe": "新增", "defaultCheck": true},
  //           { "action": "delete", "describe": "删除", "defaultCheck": true },
  //           { "action": "update", "describe": "修改", "defaultCheck": true },
  //           { "action": "query", "describe": "查询", "defaultCheck": true },
  //           { "action": "export", "describe": "导出", "defaultCheck": true },
  //           { "action": "reload", "describe": "刷新", "defaultCheck": true },
  //           { "action": "seniorSearch", "describe": "高级查询", "defaultCheck": true },
  //           { "action": "column", "describe": "列", "defaultCheck": true },
  //           { "action": "cardView", "describe": "卡片视图", "defaultCheck": true }
  //         ]
  //       },
  //       { 
  //         pathName: '/example/e3',
  //         name: 'E3',
  //         actionList: [
  //           { "action": "add", "describe": "新增", "defaultCheck": true},
  //           { "action": "delete", "describe": "删除", "defaultCheck": true },
  //           { "action": "update", "describe": "修改", "defaultCheck": true },
  //           { "action": "query", "describe": "查询", "defaultCheck": true },
  //           { "action": "export", "describe": "导出", "defaultCheck": true },
  //           { "action": "reload", "describe": "刷新", "defaultCheck": true },
  //           { "action": "seniorSearch", "describe": "高级查询", "defaultCheck": true },
  //           { "action": "column", "describe": "列", "defaultCheck": true },
  //           { "action": "cardView", "describe": "卡片视图", "defaultCheck": true }
  //         ]
  //       }
  //     ],
  //     'actionList': null,
  //     'dataAccess': null
  //   },
  //   {
  //     'roleId': 'admin',
  //     'permissionId': 'user',
  //     'permissionName': '用户管理',
  //     'actions': '[]',
  //     'actionEntitySet': [],
  //     'actionList': null,
  //     'dataAccess': null
  //   }
  // ]
  

  // 创建 role d对象 - 2
  roleObj.permissions.push({
    'roleId': 'admin',
    'permissionId': 'support',
    'permissionName': '超级模块',
    'actions': '[{"action":"add","defaultCheck":false,"describe":"新增"},{"action":"import","defaultCheck":false,"describe":"导入"},{"action":"get","defaultCheck":false,"describe":"详情"},{"action":"update","defaultCheck":false,"describe":"修改"},{"action":"delete","defaultCheck":false,"describe":"删除"},{"action":"export","defaultCheck":false,"describe":"导出"}]',
    'actionEntitySet': [
      { 'action': 'add', 'describe': '新增', 'defaultCheck': false }, 
      { 'action': 'import', 'describe': '导入', 'defaultCheck': false}, 
      { 'action': 'get', 'describe': '详情', 'defaultCheck': false}, 
      { 'action': 'update', 'describe': '修改', 'defaultCheck': false }, 
      { 'action': 'delete', 'describe': '删除', 'defaultCheck': false }, 
      { 'action': 'export', 'describe': '导出', 'defaultCheck': false}
    ],
    'actionList': null,
    'dataAccess': null
  })
  newPermissonStructure.role = roleObj
  return newPermissonStructure
}